package com.synergy.stockflow.production.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.synergy.stockflow.production.create.ReportDetailVo;
import com.synergy.stockflow.production.entity.ProductionReport;
import com.synergy.stockflow.production.mapper.ProductionReportMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.synergy.stockflow.production.entity.ProductionReportDetail;
import com.synergy.stockflow.production.mapper.ProductionReportDetailMapper;
import com.synergy.stockflow.production.service.ProductionReportDetailService;
@Service
public class ProductionReportDetailServiceImpl extends ServiceImpl<ProductionReportDetailMapper, ProductionReportDetail> implements ProductionReportDetailService{

    @Autowired
    private ProductionReportMapper productionReportMapper;

    @Override
    public List<ReportDetailVo> selectByReportId(String reportId){
        return baseMapper.selectByReportId(reportId);
    }

    @Override
    public void batchUpdateDetails(List<ProductionReportDetail> details) {
        if (details == null || details.isEmpty()) {
            return;
        }

        String reportId = null;
        for (ProductionReportDetail detail : details) {
            LambdaUpdateWrapper<ProductionReportDetail> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(ProductionReportDetail::getDetailId, detail.getDetailId())
                    .set(ProductionReportDetail::getReportedQuantity, detail.getReportedQuantity())
                    .set(ProductionReportDetail::getQualifiedQuantity, detail.getQualifiedQuantity())
                    .set(ProductionReportDetail::getDefectQuantity, detail.getDefectQuantity());

            baseMapper.update(null, updateWrapper);

            if (reportId == null) {
                reportId = detail.getReportId();
            }
        }

        // 更新主表汇总数据
        if (reportId != null) {
            updateReportSummary(reportId);
        }
    }

    /**
     * 更新主表汇总数据
     */
    private void updateReportSummary(String reportId) {
        // 获取所有明细的汇总数据
        List<ProductionReportDetail> details = baseMapper.selectByRId(reportId);

        BigDecimal totalReportedQuantity = BigDecimal.ZERO;
        BigDecimal totalQualifiedQuantity = BigDecimal.ZERO;
        BigDecimal totalDefectQuantity = BigDecimal.ZERO;

        for (ProductionReportDetail detail : details) {
            totalReportedQuantity = totalReportedQuantity.add(detail.getReportedQuantity());
            totalQualifiedQuantity = totalQualifiedQuantity.add(detail.getQualifiedQuantity());
            totalDefectQuantity = totalDefectQuantity.add(detail.getDefectQuantity());
        }

        // 更新主表
        LambdaUpdateWrapper<ProductionReport> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ProductionReport::getReportId, reportId)
                .set(ProductionReport::getTotalQuantity, totalReportedQuantity)
                .set(ProductionReport::getQualifiedQuantity, totalQualifiedQuantity)
                .set(ProductionReport::getDefectQuantity, totalDefectQuantity);

        productionReportMapper.update(null, updateWrapper);
    }

}
